<template>
  <div>
    <h1></h1>
    <el-input placeholder="请输入内容" v-model="keyword" clearable @change="lbadd" style="width:300px"></el-input>
         <!--选择器  -->
       <el-select v-model="user_level_id" placeholder="请选择">
        <el-option
          v-for="item in user_level"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    <el-button type="primary" @click="lbadd">搜索</el-button>
    <el-button type="primary" @click="lbinVisible=true" >添加</el-button>
    <h1></h1>
    <template>
      <el-table :data="lblist" style="width: 100%">
        <el-table-column prop="date" label="会员" width="180">
          <template slot-scope="scope">
            <el-image :src="scope.row.avatar"  style="width: 100px; height: 100px" />
            {{scope.row.username}}
          </template>
        </el-table-column>
        <el-table-column prop="user_level.name" label="会员等级" width="180"></el-table-column>
        <el-table-column  label="注册时间">
          <template slot-scope="scope" >
            注册时间：{{scope.row.create_time}}
          </template>
        </el-table-column>


        <el-table-column  label="状态">
          <template slot-scope="scope" >
             <el-button type="success" v-if="scope.row.status==0">成功</el-button>
               <el-button type="danger" v-else>禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
             <el-button type="text" @click="xiu(scope.row)" >修改</el-button>
               <el-button type="text" @click="del(scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
        
      </el-table>
    </template>
    <!-- 添加 -->
    <el-dialog title="添加" :visible.sync="lbinVisible">
  <el-form :model="lbin">
    <el-form-item label="用户名" >
      <el-input v-model="lbin.username" ></el-input>
    </el-form-item>
    <el-form-item label="密码" >
      <el-input v-model="lbin.password" ></el-input>
    </el-form-item>
    <el-form-item label="昵称" >
      <el-input v-model="lbin.nickname" ></el-input>
    </el-form-item>
    <el-form-item label="头像" >
      <!-- <el-input v-model="lbin.avatar" ></el-input> -->
      <el-button icon="el-icon-plus" size="small"></el-button>
      
    </el-form-item>
    <el-form-item label="会员ID" >
      <el-select v-model="lbin.user_level_id" placeholder="请选择">
        <el-option
          v-for="item in user_level"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="手机号" >
      <el-input v-model="lbin.phone" ></el-input>
    </el-form-item>
    <el-form-item label="邮箱" >
      <el-input v-model="lbin.email" ></el-input>
    </el-form-item>
    <el-form-item label="是否" >
      <!-- <el-input v-model="lbin.status" ></el-input> -->
        <el-radio v-model="lbin.status" label="0" border>启用</el-radio>
        <el-radio v-model="lbin.status" label="1" border>禁用</el-radio>
    </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="lbinVisible = false">取 消</el-button>
    <el-button type="primary" @click="lbtian">确 定</el-button>
  </div>
</el-dialog>


<!-- 修改 -->
    <el-dialog title="修改" :visible.sync="lbxiu">
  <el-form :model="lbgai">
    <el-form-item label="用户名" >
      <el-input v-model="lbgai.username" ></el-input>
    </el-form-item>
    <el-form-item label="密码" >
      <el-input v-model="lbgai.password" ></el-input>
    </el-form-item>
    <el-form-item label="昵称" >
      <el-input v-model="lbgai.nickname" ></el-input>
    </el-form-item>
    <el-form-item label="头像" >
      <!-- <el-input v-model="lbgai.avatar" ></el-input> -->
      <el-button icon="el-icon-plus" size="small"></el-button>
    </el-form-item>
    <el-form-item label="会员ID" >

      
      <!-- <el-input v-model="lbgai.user_level_id" ></el-input> -->
      <el-select v-model="lbgai.user_level_id" placeholder="请选择">
        <el-option
          v-for="item in user_level"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="手机号" >
      <el-input v-model="lbgai.phone" ></el-input>
    </el-form-item>
    <el-form-item label="邮箱" >
      <el-input v-model="lbgai.email" ></el-input>
    </el-form-item>
    <el-form-item label="是否" >
      <!-- <el-input v-model="lbgai.status" ></el-input> -->
        <el-radio v-model="lbgai.status" label="0" border>启用</el-radio>
        <el-radio v-model="lbgai.status" label="1" border>禁用</el-radio>
    </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="lbxiu = false">取 消</el-button>
    <el-button type="primary" @click="xiugai">确 定</el-button>
  </div>
</el-dialog>
 <!-- 分页器 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals">
    </el-pagination>
  </div>
</template>


<script>
export default {
  name: "",
  props: {},
  data() {
    return {
      page: 1,
      limit: 5,
      keyword: "",
      user_level_id: "",
      totals:0,
      lblist: [],
      user_level:[],
      // 添加
      lbinVisible:false,
      lbin:{
        username:"",
        password:"",
        nickname:'',
        avatar:'',
        user_level_id:"",
        phone:"",
        email:"",
        status:"0"
      },
      // 修改
      lbxiu:false,
      lbgai:{
        id:'',
        username:"",
        password:"",
        nickname:'',
        avatar:'',
        user_level_id:"",
        phone:"",
        email:"",
        status:""
      },
     
    };
  },
  components: {},
  computed: {},
  mounted() {
    this.lbadd()
  },
  methods: {
    // 页面渲染
    lbadd(){
      this.$axios
      .get(
        `/admin/user/${this.page}?limit=${this.limit}&keyword=${this.keyword}&user_level_id=${this.user_level_id}`
      )
      .then(res => {
        console.log(res);
        this.lblist = res.data.list;
        this.user_level = res.data.user_level;
        this.totals = res.data.totalCount;
      })
      .catch(error => {
        console.log(error.response.data);
        return false
      });
    },
    // 添加
    lbtian(){
      this.$axios.post('/admin/user',this.lbin).then(res=>{
        console.log(res);
        this.$message.success(res.msg)
        this.lbinVisible=false
        this.lbadd()
      }).catch(error=>{
        console.log(error.response)
        this.$message.error(error.response.data.msg)
        return false
      })
    },
    // 修改
    xiu(id){
      console.log(id)
      this.lbgai = id
      this.lbxiu = true
    },
    xiugai(){
      this.$axios.post(`/admin/user/${this.lbgai.id}`,this.lbgai).then(res=>{
        console.log(res);
        this.$message.success('修改成功');
        this.lbxiu=false;
        this.lbadd();
      }).catch(error=>{
        console.log(error.response.data)
        this.$message.error(error.response.data.msg)
        return false
      })
    },
    // 删除
    del(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.post(`admin/user/${id}/delete`).then(res=>{
            console.log(res)
            this.$message.success(res.msg)
            this.lbadd()
          }) .catch(error=>{
              console.log(error.response.data)
              this.$message.error(error.response.data.msg)
              return false
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    
    handleSizeChange(limit){
      // console.log(limit)
        this.limit = limit
        this.lbadd()
      },
      handleCurrentChange(page){
        //  console.log(page)
        this.page = page
        this.lbadd()
      },
  }
};
</script>


<style scoped>
</style>